const { Controller } = require('egg')

const indexRule = {
	pageSize: 'string',
	pageNum: 'string',
}
const createRule = {
	title: 'string',
	content: 'string',
	authorId: 'int',
	cover: 'string',
	tagId: 'int',
}
const updateRule = {
	title: 'string',
	content: 'string',
	cover: 'string',
	tagId: 'int',
}

/**
 * @controller Article 文章
 */
class ArticleController extends Controller {
	/**
	 * @summary 文章列表
	 * @description 文章列表
	 * @router get /admin/articles
	 * @request query number pageSize eg:10 页尺寸
	 * @request query number pageNum eg:1 页码
	 */
	async index() {
		const ctx = this.ctx
		const { username } = ctx.decode
		try {
			ctx.validate(indexRule, ctx.request.query)
			const { count, rows } = await ctx.service.article.index(
				ctx.request.query,
				username
			)
			const data = {
				list: rows,
				total: count,
			}
			ctx.helper.success(data)
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 创建文章
	 * @description 创建文章
	 * @router post /admin/articles
	 * @request body createArticleRequest
	 */
	async create() {
		const ctx = this.ctx
		try {
			ctx.validate(createRule, ctx.request.body)
			const status = await ctx.service.article.create(ctx.request.body)
			if (status === 'success') {
				ctx.helper.success('创建成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 删除文章
	 * @description 删除文章
	 * @router delete /admin/articles/{id}
	 * @request path string *id 标识
	 */
	async destroy() {
		const ctx = this.ctx
		try {
			const { id } = ctx.params
			const status = await ctx.service.article.destroy(id)
			if (status === 'success') {
				ctx.helper.success('删除成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 更新文章
	 * @description 更新文章
	 * @router put /admin/articles/{id}
	 * @request path string *id 标识
	 * @request body updateArticleRequest
	 */
	async update() {
		const ctx = this.ctx
		try {
			ctx.validate(updateRule, ctx.request.body)
			const { id } = ctx.params
			const status = await ctx.service.article.update(
				id,
				ctx.request.body
			)
			if (status === 'success') {
				ctx.helper.success('更新成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 查看文章
	 * @description 查看文章
	 * @router get /admin/articles/{id}
	 * @request path string *id 标识
	 */
	async show() {
		const ctx = this.ctx
		try {
			const { id } = ctx.params
			const data = await ctx.service.article.show(id)
			if (data.status === 'success') {
				ctx.helper.success(data.body)
			} else {
				ctx.helper.error(data.status)
			}
		} catch (e) {
			ctx.helper.error(e)
		}
	}
	/**
	 * @summary 文章提交审核
	 * @description 文章提交审核
	 * @router put /admin/auditArticle/{id}
	 * @request path string *id 标识
	 * @request body updateAuditArticleRequest
	 *
	 */
	async updateAudit() {
		const ctx = this.ctx
		try {
			const { id } = ctx.params
			const status = await ctx.service.article.updateAudit(
				id,
				ctx.request.body
			)
			if (status === 'success') {
				ctx.helper.success('提交成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {}
	}
	/**
	 * @summary 文章首页展示
	 * @description 文章首页展示
	 * @router put /admin/homePageArticle/{id}
	 * @request path string *id 标识
	 * @request body updateAuditArticleRequest
	 *
	 */
	async updateHomePage() {
		const ctx = this.ctx
		const { username } = ctx.decode
		try {
			const { id } = ctx.params
			const status = await ctx.service.article.updateHomePage(
				id,
				ctx.request.body,
				username
			)
			if (status === 'success') {
				ctx.helper.success('修改成功')
			} else {
				ctx.helper.error(status)
			}
		} catch (e) {}
	}
}

module.exports = ArticleController
